package com.localcache.server.init;

import cn.hutool.system.SystemPropsKeys;
import cn.hutool.system.SystemUtil;
import com.localcache.server.config.CacheReportServerConfig;
import com.localcache.server.service.DlcCacheServerService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 服务启动 初始化操作
 *
 * @author chennengcheng
 * @date 2021-09-13 10:59
 * @remark
 */
@Component
public class Initializer implements CommandLineRunner {

    @Resource
    private CacheReportServerConfig cacheReportServerConfig;

    private static String systemPath;

    public static String getSystemPath() {
        return systemPath;
    }

    @Override
    public void run(String... args) throws Exception {
        String systemName = SystemUtil.get(SystemPropsKeys.OS_NAME);
        if (systemName.toLowerCase().contains("windows")) {
            systemPath = DlcCacheServerService.class.getResource("/").getPath();
        }else if (systemName.toLowerCase().contains("linux")) {
            systemPath = "/tmp/";
        }

        new Thread(() -> {
            CacheManagerServer cacheManagerServer = new CacheManagerServer(cacheReportServerConfig.getDlcServerPort());
            cacheManagerServer.initNettyServer();
        }).start();
    }
}
